package com.sotb.receiver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
/**
 * 
 * @author 永能
 *
 */
public class HistoryListActivity extends ListActivity {
	//private Button insert = null;
	//Button i = null;
	TextView hintText = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_list);
		hintText = (TextView)findViewById(R.id.hintText);
		
		
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SimpleAdapter adapter = new SimpleAdapter(this, list(), R.layout.history_list_item,         //创建适配器，用于将几个控件放在一起，做成历史list
				new String[]{"isCompleted", "number", "time"},
				new int[]{R.id.isCompleted, R.id.number, R.id.time});
		setListAdapter(adapter);
		/*
		insert = (Button)findViewById(R.id.insertButton);
		insert.setOnClickListener(new OnClickListener(){

		//用于测试数据库操作是否正确的按钮，正式版应当删除
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DatabaseHelper dbHelper = new DatabaseHelper(HistoryListActivity.this, "receiver_db", Constant.DB_VERSION);    //创建数据库帮助类
				System.out.println("1");
				SQLiteDatabase db = dbHelper.getWritableDatabase();                                                           //获取数据库
				System.out.println("2");
				HistoryDbUtil dbUtil = new HistoryDbUtil(db);                                   //自己写的数据库帮助类，用于封装各种数据库操作
				dbUtil.updateReceiveComplete("1010");
				//dbUtil.insertData("1010", "1.343,432.35234");
			}}
		);*/
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
	}
	
     
	private List<Map<String, Object>> list(){
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		
		DatabaseHelper dbHelper = new DatabaseHelper(HistoryListActivity.this,"receiver_db", Constant.DB_VERSION);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		try{
			Cursor cursor = db.query("history_list", new String[]{"id", "number", "time", "location", "isCompleted"}, null, null, null, null, null);   //获取数据库查询数据的光标位置
			cursor.moveToNext();
			while(cursor != null ){
				map = new HashMap<String, Object>();
				int isCompleted = cursor.getInt(cursor.getColumnIndex("isCompleted"));
				String number = cursor.getString(cursor.getColumnIndex("number"));
				String time = cursor.getString(cursor.getColumnIndex("time"));
				String location = cursor.getString(cursor.getColumnIndex("location"));
				System.out.println("location=" + location);
				if( isCompleted == Constant.RECEIVE_COMPLETE )
					map.put("isCompleted", R.drawable.complete);
					//map.put("isCompleted", android.R.drawable.presence_online);
				else 
					map.put("isCompleted", R.drawable.warn);
					//map.put("isCompleted", android.R.drawable.stat_sys_warning);
				map.put("number", number);
				map.put("time", time);
				
				list.add(map);
				cursor.moveToNext();                      //移动光标
				hintText.setVisibility(View.INVISIBLE);
			}
			db.close();
		}catch(Exception e){
			System.out.println("chaxun cuodfdffu");
			e.printStackTrace();
		}
		
		return list;
	}
	
	//点击列表项，传递坐标信息
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		DatabaseHelper dbHelper = new DatabaseHelper(HistoryListActivity.this,"receiver_db", Constant.DB_VERSION);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		try{
		    //根据点击的列表项position获取数据库内相应数据的id
			Cursor cursor = db.query("history_list", new String[]{"id", "number", "time", "location", "isCompleted"}, "id=?", new String[]{position+1+""}, null, null, null);
			cursor.moveToNext();                          
			int idTemp = cursor.getInt(cursor.getColumnIndex("id"));
			String location = cursor.getString(cursor.getColumnIndex("location"));           //根据查询数据的列名获取数据
			String number = cursor.getString(cursor.getColumnIndex("number"));
			System.out.println(idTemp+"");
			Intent intent = new Intent();
			intent.putExtra("location", location);              //传递的坐标
			intent.putExtra("number", number);                 //传递的号码
			intent.setClass(this, HistoryMapActivity.class);
			startActivity(intent);                                       //开启HistoryMapActivity
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
